package com.astn.bks;

import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import com.astn.adapter.booking.ProviderAdapter;
import com.astn.entity.booking.QuoteUser;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.animation.AnimationUtils;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.ViewFlipper;

public class Home extends Activity implements OnTouchListener {

	private static int POST_REQUEST = 1988;

	private ViewFlipper homeFlipper;
	private ListView lsvTopProvider, lsvTopDestince;
	private ArrayList<QuoteUser> lstUser;// Top provider

	private float touchDown = 0;
	private float touchUp = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.home);
		homeFlipper = (ViewFlipper) findViewById(R.id.fliHome);
		lstUser = new ArrayList<QuoteUser>();
		lstUser.add(new QuoteUser("QuangND1", "quang0804@gmail.com", 1,
				"0985642570", "Hai Duong"));
		lstUser.add(new QuoteUser("QuangND2", "quang0804@gmail.com", 1,
				"0985642570", "Hai Duong"));
		lstUser.add(new QuoteUser("QuangND3", "quang0804@gmail.com", 1,
				"0985642570", "Hai Duong"));
		lstUser.add(new QuoteUser("QuangND4", "quang0804@gmail.com", 1,
				"0985642570", "Hai Duong"));
		lstUser.add(new QuoteUser("QuangND5", "quang0804@gmail.com", 1,
				"0985642570", "Hai Duong"));
		lstUser.add(new QuoteUser("QuangND6", "quang0804@gmail.com", 1,
				"0985642570", "Hai Duong"));
		lstUser.add(new QuoteUser("QuangND7", "quang0804@gmail.com", 1,
				"0985642570", "Hai Duong"));
		lstUser.add(new QuoteUser("QuangND8", "quang0804@gmail.com", 1,
				"0985642570", "Hai Duong"));
		lstUser.add(new QuoteUser("QuangND9", "quang0804@gmail.com", 1,
				"0985642570", "Hai Duong"));
		lstUser.add(new QuoteUser("QuangND10", "quang0804@gmail.com", 1,
				"0985642570", "Hai Duong"));
		lstUser.add(new QuoteUser("QuangND11", "quang0804@gmail.com", 1,
				"0985642570", "Hai Duong"));
		lstUser.add(new QuoteUser("QuangND12", "quang0804@gmail.com", 1,
				"0985642570", "Hai Duong"));
		lstUser.add(new QuoteUser("QuangND13", "quang0804@gmail.com", 1,
				"0985642570", "Hai Duong"));
		lstUser.add(new QuoteUser("QuangND14", "quang0804@gmail.com", 1,
				"0985642570", "Hai Duong"));
		lstUser.add(new QuoteUser("QuangND15", "quang0804@gmail.com", 1,
				"0985642570", "Hai Duong"));
		lstUser.add(new QuoteUser("QuangND15", "quang0804@gmail.com", 1,
				"0985642570", "Hai Duong"));
		lstUser.add(new QuoteUser("QuangND15", "quang0804@gmail.com", 1,
				"0985642570", "Hai Duong"));
		lstUser.add(new QuoteUser("QuangND15", "quang0804@gmail.com", 1,
				"0985642570", "Hai Duong"));
		lstUser.add(new QuoteUser("QuangND15", "quang0804@gmail.com", 1,
				"0985642570", "Hai Duong"));
		lstUser.add(new QuoteUser("QuangND15", "quang0804@gmail.com", 1,
				"0985642570", "Hai Duong"));
		lstUser.add(new QuoteUser("QuangND15", "quang0804@gmail.com", 1,
				"0985642570", "Hai Duong"));
		lstUser.add(new QuoteUser("QuangND15", "quang0804@gmail.com", 1,
				"0985642570", "Hai Duong"));
		lstUser.add(new QuoteUser("QuangND15", "quang0804@gmail.com", 1,
				"0985642570", "Hai Duong"));
		lstUser.add(new QuoteUser("QuangND15", "quang0804@gmail.com", 1,
				"0985642570", "Hai Duong"));
		LinearLayout layHome = (LinearLayout) findViewById(R.id.layHome);
		lsvTopProvider = (ListView) findViewById(R.id.lsvTopProvider);
		lsvTopDestince = (ListView) findViewById(R.id.lsvTopDestince);
		layHome.setOnTouchListener((OnTouchListener) this);
		// lsvTopProvider.setOnTouchListener((OnTouchListener) this);
		lsvTopProvider.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {

			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub

			}

		});
		// lsvTopProvider.setOnClickListener(new OnClickListener() {
		//
		// @Override
		// public void onClick(View arg0) {
		// Toast.makeText(getApplicationContext(), "Clicked",
		// Toast.LENGTH_SHORT).show();
		// }
		//
		// });
		// lsvTopDestince.setOnTouchListener((OnTouchListener) this);
		ProviderAdapter providerAdap = new ProviderAdapter(
				getApplicationContext(), lstUser);
		lsvTopProvider.setAdapter(providerAdap);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.optmenucustomer, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.mnCustomerPostRequest:
			// Select post request
			postRequest();
			return true;
		case R.id.mnCustomerHistory:
			// View history
			history();
			return true;
		case R.id.mnCustomerMessage:
			// Message
			message();
			return true;
		case R.id.mnCustomerSearch:
			// Search
			return true;
		case R.id.mnCustomerPayment:
			// Payment infomation
			return true;
		case R.id.mnCustomerHelp:
			// Help
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	private void postRequest() {
		Intent t = new Intent(Home.this, PostRequest.class);
		startActivityForResult(t, POST_REQUEST);
	}

	private void history() {

	}

	private void message() {

	}

	private void search() {

	}

	private void payment() {

	}

	private void help() {

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			touchDown = event.getX();
			break;
		case MotionEvent.ACTION_UP:
			touchUp = event.getX();
			if (touchUp < touchDown) {
				homeFlipper.setAnimation(AnimationUtils.loadAnimation(
						Home.this, android.R.anim.slide_out_right));

				homeFlipper.showPrevious();
			}
			if (touchUp > touchDown) {
				homeFlipper.setAnimation(AnimationUtils.loadAnimation(
						Home.this, android.R.anim.slide_in_left));
				homeFlipper.showNext();
			}
			break;

		default:
			break;
		}
		return true;
	}

}
